<template>
  <div>

    <!-- 新增弹出框 -->
    <el-button type="primary" style="margin:10px 30px;float:right; " @click="openAddPage">新增</el-button>
    <!-- 新增弹出框底层 -->
    <el-button type="danger" @click="deleteSelect" style="margin:10px 5px; float:right;">删除选中</el-button>
    <el-table
      :data="tableData"
      style="width: 100%"
      :row-class-name="tableRowClassName"
      @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        type="index"
        label="序号"
        width="50">
      </el-table-column>
      <el-table-column
        prop="taskNo"
        label="任务单单号">
      </el-table-column>
      <el-table-column
        prop="taskName"
        label="任务单名称">
      </el-table-column>
      <el-table-column
        prop="workDate"
        label="任务单日期">
      </el-table-column>
      <el-table-column
        prop="examineStatusMessage"
        label="审核状态描述">
      </el-table-column>
      <el-table-column
        prop="taskStatusMessage"
        label="最新审批结果">
      </el-table-column>
      <el-table-column
        prop="founderName"
        label="签发人">
      </el-table-column>
      <el-table-column
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="detail(scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="page.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
  import axios from 'axios'
  import { mapGetters } from 'vuex'
  import { addMachinery, deleteMachineryList } from '@/api/construct'

  var pageParams = { size: 10, page: 1 }
  export default {
    computed: {
      ...mapGetters([
        'currentProject',
        'projects',
        'userInfo'
      ])
    },
    watch: {
      currentProject: function(n, o) {
        this.initDatas()
      }
    },
    data() {
      return {
        page: {
          currentPage: 1,
          pageSize: 10,
          total: 0
        },
        tableData: [],
        /*新增弹出框操作*/
        dialogTableVisible: false,
        dialogFormVisible: false,
        form: {
          machineryName: '',
          projectId: ''
        },
        formLabelWidth: '120px',
        //////////////////////////
        /*选中删除*/
        multipleSelection: []
      }
    },
    methods: {
      //新增页面
      openAddPage() {
        this.dialogFormVisible = true
      },
      resetForm(form) {
        /*debugger
        this.$refs[form].resetFields();*/
        this.dialogFormVisible = false
      },
      detail(row) {
        //编辑页面
        this.form = Object.assign({}, row)
        this.dialogFormVisible = true
      },
      //提交
      submit(form) {
        addMachinery(this.form).then(res => {
          this.initDatas()
          this.dialogFormVisible = false
          this.$message.success(res.message)
        }).catch(res => {
          this.$message.error(res.message)
        })
      },
      //删除选中
      deleteSelect(row) {
        deleteMachineryList(this.multipleSelection).then(res => {
          this.$message.success(res.message)
          this.initDatas()
        }).catch(res => {
          this.$message.error(res.message)
        })
      },
      //删除按钮
      deleteSelects(row) {
        deleteMachineryList([row]).then(res => {
          this.$message.success(res.message)
          this.initDatas()
        }).catch(res => {
          this.$message.error(res.message)
        })
      },
      handleSelectionChange(val) {
        this.multipleSelection = val
      },
      initDatas() {
        let data = new FormData()
        data.append('size', pageParams.size)
        data.append('page', pageParams.page)
        axios.post(`http://bim.jaso.com.cn/api/taskSheets/taskList`, data)
          .then(res => {
            this.tableData = res.data.data.content
            this.page.total = res.data.data.totalElements
          })
      },
      tableRowClassName({ row, rowIndex }) {
        if (rowIndex === 1) {
          return 'warning-row'
        } else if (rowIndex === 3) {
          return 'success-row'
        }
        return ''
      },
      handleSizeChange(val) {
        pageParams.size = val
        this.initDatas()
        console.log(`每页 ${val} 条`)
      },
      handleCurrentChange(val) {
        pageParams.page = val
        this.initDatas()
        console.log(`当前页: ${val}`)
      }
    },
    created() {
      this.initDatas()
    }
  }
</script>
